<template>
  <view class="container">
    <!-- 分类标签 -->
    <scroll-view class="category-tags" scroll-x>
      <view class="tag-list">
        <view class="tag" 
              :class="{ active: activeTag === 'recommend' }" 
              @tap="switchTag('recommend')">推荐</view>
        <view class="tag" 
              :class="{ active: activeTag === 'guide' }" 
              @tap="switchTag('guide')">攻略</view>
        <view class="tag" 
              :class="{ active: activeTag === 'travel' }" 
              @tap="switchTag('travel')">游记</view>
        <view class="tag" 
              :class="{ active: activeTag === 'food' }" 
              @tap="switchTag('food')">美食</view>
        <view class="tag" 
              :class="{ active: activeTag === 'hotel' }" 
              @tap="switchTag('hotel')">酒店</view>
        <view class="tag" 
              :class="{ active: activeTag === 'shopping' }" 
              @tap="switchTag('shopping')">购物</view>
      </view>
    </scroll-view>

    <!-- 文章列表 -->
    <view class="article-list">
      <view class="article-item" v-for="(item, index) in articles" :key="index" @tap="goToDetail(item)">
        <image class="cover" :src="item.coverImage" mode="aspectFill"></image>
        <view class="content">
          <view class="title">{{item.title}}</view>
          <view class="brief" v-if="item.content">{{item.content}}</view>
          <view class="meta">
            <image class="avatar" :src="getAvatarUrl(item)" mode="aspectFill"></image>
            <text class="author">{{getNickname(item) || '山海用户'}}</text>
            <text class="views">{{item.views}}阅读</text>
            <text class="likes">{{item.likes}}点赞</text>
          </view>
        </view>
      </view>
      
      <!-- 加载更多 -->
      <view class="load-more" v-if="hasMore" @tap="loadMore">
        <text>加载更多</text>
      </view>
      <view class="no-more" v-else>
        <text>没有更多了</text>
      </view>
    </view>
  </view>
</template>

<script>
import request from '@/utils/request'
import { SERVICE_PREFIX } from '@/utils/request'

export default {
  data() {
    return {
      activeTag: 'recommend',
      articles: [],
      page: 0,
      size: 10,
      hasMore: true,
      isLoading: false
    }
  },
  onLoad() {
    this.loadArticles()
  },
  // 下拉刷新
  onPullDownRefresh() {
    this.refreshArticles()
  },
  // 触底加载更多
  onReachBottom() {
    if (this.hasMore && !this.isLoading) {
      this.loadMore()
    }
  },
  methods: {
    // 切换标签
    switchTag(tag) {
      if (this.activeTag !== tag) {
        this.activeTag = tag
        this.refreshArticles()
      }
    },
    
    // 刷新文章列表
    refreshArticles() {
      this.page = 0
      this.articles = []
      this.hasMore = true
      this.loadArticles()
      uni.stopPullDownRefresh()
    },
    
    // 加载文章列表
    async loadArticles() {
      if (this.isLoading) return
      
      this.isLoading = true
      
      try {
        uni.showLoading({
          title: '加载中...'
        })
        
        const params = {
          page: this.page,
          size: this.size,
          type: this.activeTag === 'recommend' ? null : this.activeTag
        }
        
        // 调用实际的API接口
        const res = await request.article.get('/api/article/getArticleList', params)
        
        uni.hideLoading()
        
        if (res.code === 200 && res.data) {
          const newArticles = res.data.records || []
          
          this.articles = [...this.articles, ...newArticles]
          
          // 判断是否还有更多
          if (newArticles.length < this.size || this.page >= res.data.pages - 1) {
            this.hasMore = false
          } else {
            this.page++
          }
        } else {
          uni.showToast({
            title: res.errorMessage || '获取游记列表失败',
            icon: 'none'
          })
        }
      } catch (error) {
        console.error('获取游记列表失败:', error)
        uni.hideLoading()
        uni.showToast({
          title: '获取游记列表失败',
          icon: 'none'
        })
      } finally {
        this.isLoading = false
      }
    },
    
    // 加载更多
    loadMore() {
      if (!this.isLoading && this.hasMore) {
        this.loadArticles()
      }
    },
    
    // 跳转到详情页
    goToDetail(item) {
      // 将完整的文章数据存储到缓存中，方便详情页使用
      uni.setStorageSync('currentArticle', item);
      
      uni.navigateTo({
        url: `/pages/article/detail?id=${item.articleId}`
      })
    },
    
    // 获取用户头像
    getAvatarUrl(item) {
      return item.user && item.user.avatarUrl ? item.user.avatarUrl : '/static/images/avatar.gif';
    },
    
    // 获取用户昵称
    getNickname(item) {
      return item.user && item.user.nickname ? item.user.nickname : '山海用户';
    }
  }
}
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  background: #f8f8f8;
}

.category-tags {
  background: #fff;
  padding: 20rpx 0;
  position: sticky;
  top: 0;
  z-index: 100;
  
  .tag-list {
    display: flex;
    padding: 0 20rpx;
    white-space: nowrap;
  }
  
  .tag {
    padding: 10rpx 30rpx;
    font-size: 28rpx;
    color: #666;
    margin-right: 20rpx;
    
    &.active {
      color: #4080ff;
      font-weight: bold;
      position: relative;
      
      &::after {
        content: '';
        position: absolute;
        bottom: -4rpx;
        left: 50%;
        transform: translateX(-50%);
        width: 40rpx;
        height: 4rpx;
        background: #4080ff;
        border-radius: 2rpx;
      }
    }
  }
}

.article-list {
  padding: 20rpx;
  
  .article-item {
    background: #fff;
    border-radius: 12rpx;
    margin-bottom: 20rpx;
    overflow: hidden;
    
    .cover {
      width: 100%;
      height: 360rpx;
    }
    
    .content {
      padding: 20rpx;
      
      .title {
        font-size: 32rpx;
        color: #333;
        margin-bottom: 16rpx;
      }
      
      .brief {
        font-size: 28rpx;
        color: #666;
        margin-bottom: 16rpx;
        line-height: 1.5;
        // 最多显示2行
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      
      .meta {
        display: flex;
        align-items: center;
        
        .avatar {
          width: 40rpx;
          height: 40rpx;
          border-radius: 50%;
          margin-right: 10rpx;
        }
        
        .author {
          font-size: 24rpx;
          color: #666;
          margin-right: 20rpx;
        }
        
        .views {
          font-size: 24rpx;
          color: #999;
          margin-right: 20rpx;
        }
        
        .likes {
          font-size: 24rpx;
          color: #999;
        }
      }
    }
  }
  
  .load-more, .no-more {
    text-align: center;
    padding: 20rpx 0;
    
    text {
      font-size: 28rpx;
      color: #999;
    }
  }
}
</style> 